package com.lxq.live.util;

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;

@Component
public class CommonUtil {

    public static String VIRTUAL_ROOT = "D:/live_repository/";
    public static String VIRTUAL_PUBLIC = VIRTUAL_ROOT + "public/";
    public static String VIRTUAL_USER = VIRTUAL_ROOT + "user_zone/";
    public static String RELATE_VIRTUAL_ROOT = "/live_repository/";

    public String generateUserId() {
        var stringBuilder = new StringBuilder();
        for (var i = 1; i <= 10; i++) {
            stringBuilder.append(new Random().nextInt(10));
        }
        return stringBuilder.toString();
    }

    public String passwordEncoder(String password){
        var bCryptPasswordEncoder = new BCryptPasswordEncoder();
        return bCryptPasswordEncoder.encode(password);
    }

    public String getRelatePathOfUser(String userId) {
        return RELATE_VIRTUAL_ROOT + "user_zone/" + userId + "/";
    }

    /*public void mkrRepository(){
        var virtualPublic=new File(VIRTUAL_PUBLIC);
        if (!virtualPublic.exists()){
            System.out.println("路径 "+virtualPublic+virtualPublic.mkdirs()+" 创建成功");
        }else{
            System.out.println("路径 "+virtualPublic+virtualPublic.mkdirs()+" 创建失败");
        }
        var virtualUser=new File(VIRTUAL_USER);
        if (!virtualUser.exists()){
            System.out.println("路径 "+VIRTUAL_USER+virtualUser.mkdirs()+" 创建成功");
        }else{
            System.out.println("路径 "+VIRTUAL_USER+virtualUser.mkdirs()+" 创建失败");
        }
    }*/

    public boolean generateScreenShot(String resourcePath, String savePath, int seek) {
        var videoFile = new File(resourcePath);
        if (!videoFile.exists()) {
            System.err.println("文件不存在");
            return false;
        }
        var ff = new FFmpegFrameGrabber(resourcePath);
        try {
            ff.start();
            var frameLength = ff.getLengthInFrames();
            System.out.println("frameLength=" + frameLength);
            var timeLength = (int) ff.getLengthInTime() / 1000 / 1000;
            System.out.println("timeLength=" + timeLength);
            var fps = frameLength / timeLength;
            System.out.println("fps=" + fps);
            var seekTo = seek * fps;
            System.out.println("seek=" + seek);
            System.out.println("seekTo=" + seekTo);
            File targetFile = new File(savePath);
            var i = 0;
            Frame frame = null;
            while (i < frameLength) {
                frame = ff.grabImage();
                if ((i == seekTo) && (frame.image != null)) {
                    break;
                }
                i++;
            }
            var converter = new Java2DFrameConverter();
            var srcBi = converter.getBufferedImage(frame);
            ImageIO.write(srcBi, savePath.substring(savePath.lastIndexOf(".") + 1), targetFile);
            ff.stop();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public void checkAndMKDir(String filePath) {
        var path = new File(filePath);
        if (!path.exists()) {
            path.mkdirs();
            System.out.println("路径“" + filePath + "”不存在，现在已经创建");
        } else {
            System.out.println("路径“" + filePath + "”已存在");
        }
    }

    public String getUUID() {
        System.out.println(UUID.randomUUID());
        return UUID.randomUUID().toString().trim().replaceAll("-", "");
    }
}
